package com.ran.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *页面跳转测试后端，用于提供数据
 */
@WebServlet("/givedata")
public class givedata extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
//          准备数据
        HashMap<String, Object> hsmp = new HashMap<>();
//        ArrayList<Object> list = new ArrayList<>();
//        list.add(hsmp.put("姓名","张三"));
//        list.add(hsmp.put("年龄","18"));
//        list.add(hsmp.put("性别","男"));--list内add的是对象，不是图
//        将数据转化为json格式发送给客户端
        hsmp.put("姓名","张三");
        hsmp.put("年龄","18");
        hsmp.put("性别","男");
        JSONObject jsonObject = new JSONObject(hsmp);
//        System.out.println("jsonObject:"+jsonObject);
//        System.out.println("jsonObject.toJsonString:"+jsonObject.toJSONString());
//        System.out.println("String.valueOf(jsonObject):"+String.valueOf(jsonObject));
        resp.getWriter().write(String.valueOf(jsonObject));
//        resp.getWriter().write("abc");
    }
}
